package com.rtf.framework.common.secure.signature.v2;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rtf.framework.common.json.JsonMapper;
import com.rtf.framework.common.util.UtilDigest;
import com.rtf.framework.common.util.UtilString;
import com.google.common.collect.Maps;

import java.util.Date;
import java.util.Map;

public class UtilSignatureParser {

    /**
     * 构建请求认证头
     * @param accessKeyId
     * @param signature
     * @return
     */
    private static String composeRequestAuthorization(String accessKeyId, String signature , Date signCreateTime) {
        Map<String,String> acMap = Maps.newLinkedHashMap() ;

        acMap.put( AppSignatureParams.AC_VERSION , "1" ) ;
        acMap.put( AppSignatureParams.AC_KEY , accessKeyId ) ;
        acMap.put( AppSignatureParams.AC_TIME , signCreateTime.getTime()+"") ;
        acMap.put( AppSignatureParams.AC_SIGNATURE , signature) ;

        return JSON.toJSONString( acMap ) ;
    }

    /**
     * 构建签名字符串
     * @param method
     * @param path
     * @return
     */
    public static String buildSignature(String method, String path , String accessSecret ) {
        Map<String,Object> signParams = Maps.newLinkedHashMap() ;

        signParams.put("method" , method.toUpperCase()) ;
        signParams.put("path" , path.toUpperCase()) ;
        signParams.put("accessSecret" , accessSecret) ;

        return UtilDigest.encodeMD5(JSON.toJSONString( signParams )) ;
    }

    /**
     *
     * @param accessKey
     * @param httpMethod
     * @param resourcePath
     * @return
     */
    public static String buildRequestAuthorization(String accessKey, String accessSecret , String httpMethod, String resourcePath) {

        Date signCreateTime = new Date() ;

        String signature = buildSignature( httpMethod , resourcePath , accessSecret) ;

        String requestAuthorization = composeRequestAuthorization( accessKey , signature , signCreateTime ) ;

        return UtilDigest.encodeBase64( requestAuthorization ) ;
    }

    /**
     * 解析请求签名
     * @param authorizationValue
     * @return
     */
    public static AppSignature parseRequestAuthorization(String authorizationValue){
        if(UtilString.isBlank( authorizationValue )){
            return null ;
        }

        String targetValue = UtilDigest.decodeBase64( authorizationValue ) ;

        JSONObject jsonObject = JsonMapper.parseObject( targetValue ) ;

        String version = jsonObject.getString( AppSignatureParams.AC_VERSION ) ;

        String accessKeyId = jsonObject.getString( AppSignatureParams.AC_KEY ) ;

        String signature = jsonObject.getString( AppSignatureParams.AC_SIGNATURE ) ;

        Date createTime = jsonObject.getDate( AppSignatureParams.AC_TIME ) ;

        AppSignature appSignature = new AppSignature() ;
        appSignature.setVersion( version ) ;
        appSignature.setAccessKeyId( accessKeyId ) ;
        appSignature.setSignature( signature ) ;
        appSignature.setCreateTime( createTime ) ;

        return appSignature ;
    }

}
